home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-06-20 | 4.1 KB | 212 lines | [TEXT/CWIE] |
- #include <Traps.h>
- #include <A4Stuff.h>
- #include "Drawing.h"
- #include <Gestalt.h>
-
- typedef pascal void (*DrawPictPP)(PicHandle, Rect*);
- typedef pascal Handle (*GetPictPP)(short);
- typedef pascal void (*InitWindPP)(void);
-
- GetPictPP oldGetPicture;
- DrawPictPP oldDrawPicture;
- InitWindPP oldInitWindows;
- short oldBitDepth;
- Boolean bypass;
- Boolean isRunning;
-
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- pascal Handle MyGetPicture(short);
- pascal void MyDrawPicture(PicHandle, Rect*);
- pascal void MyInitWindows(void);
- static void Cleanup(void);
-
- #ifdef __cplusplus
- }
- #endif
-
- Str31 extFileName;
- short extVRefNum;
- long extDirID;
- Handle myHandle;
- UniversalProcPtr myGetPictureJMPAddr, myDrawPictureJMPAddr, myInitWindowsJMPAddr;
-
- pascal void main(Handle me)
- {
- long oldA4;
-
- oldA4 = SetCurrentA4();
-
- myHandle = me;
-
- bypass = false;
- isRunning = false;
-
- {
- FCBPBRec pb;
-
- pb.ioFCBIndx = 0;
- pb.ioVRefNum = 0;
- pb.ioRefNum = CurResFile();
- pb.ioNamePtr = extFileName;
- PBGetFCBInfoSync(&pb);
-
- extVRefNum = pb.ioFCBVRefNum;
- extDirID = pb.ioFCBParID;
- }
-
- myGetPictureJMPAddr = (UniversalProcPtr) NewPtrSys(6*3); // room for 3 JMP $xxxxxxxx's
-
- if (myGetPictureJMPAddr != nil)
- {
- void* p = (void*) myGetPictureJMPAddr;
-
- *((short*) p)++ = 0x4EF9; // JMP
- *((long*) p)++ = (long) MyGetPicture;
- myDrawPictureJMPAddr = (UniversalProcPtr) p;
- *((short*) p)++ = 0x4EF9; // JMP
- *((long*) p)++ = (long) MyDrawPicture;
- myInitWindowsJMPAddr = (UniversalProcPtr) p;
- *((short*) p)++ = 0x4EF9; // JMP
- *((long*) p)++ = (long) MyInitWindows;
-
- oldGetPicture = (GetPictPP) GetToolTrapAddress(_GetPicture);
- SetToolTrapAddress(myGetPictureJMPAddr, _GetPicture);
- }
-
- SetA4(oldA4);
- }
-
- pascal Handle MyGetPicture(short id)
- {
- GetPictPP gp;
- long oldA4;
-
- oldA4 = SetCurrentA4();
-
- gp = oldGetPicture;
-
- if (!bypass && !isRunning)
- {
- short cResFile = CurResFile();
- short myFile;
- Boolean oldBypass = bypass;
-
- bypass = true;
-
- StopDraw();
-
- isRunning = false;
-
- DoneDraw();
-
- myFile = HOpenResFile(extVRefNum, extDirID, extFileName, fsCurPerm);
- UseResFile(myFile);
- if (InitDraw(id))
- bypass = oldBypass = true;
- CloseResFile(myFile);
- UseResFile(cResFile);
-
- bypass = oldBypass;
-
- if (!bypass)
- {
- UniversalProcPtr temp;
-
- temp = GetToolTrapAddress(_DrawPicture);
- if (temp != myDrawPictureJMPAddr)
- {
- oldDrawPicture = (DrawPictPP) temp;
- SetToolTrapAddress(myDrawPictureJMPAddr, _DrawPicture);
- }
-
- temp = GetToolTrapAddress(_InitWindows);
- if (temp != myInitWindowsJMPAddr)
- {
- oldInitWindows = (InitWindPP) temp;
- SetToolTrapAddress(myInitWindowsJMPAddr, _InitWindows);
- }
- }
- }
-
- SetA4(oldA4);
-
- return (*gp)(id);
- }
-
- pascal void MyDrawPicture(PicHandle pic, Rect* r)
- {
- DrawPictPP dp;
- long oldA4;
-
- oldA4 = SetCurrentA4();
- dp = (DrawPictPP) oldDrawPicture;
- SetA4(oldA4);
-
- (*dp)(pic, r);
-
- oldA4 = SetCurrentA4();
- if (!bypass && !isRunning)
- {
- // Verify that we haven't already launched the Finder
- if ((0 < LMGetCurApName()[0]) && (LMGetCurApName()[0] < sizeof(Str31)))
- Cleanup(); // If an application is now running, clean up!
- }
-
- if (!bypass && !isRunning)
- {
- GDHandle mainDev;
- Boolean oldBypass = bypass;
-
- bypass = true;
-
- mainDev = GetMainDevice();
-
- oldBitDepth = (**(**mainDev).gdPMap).pixelSize;
-
- StartDraw(r, (**mainDev).gdPMap);
-
- isRunning = true;
-
- bypass = oldBypass;
- }
- SetA4(oldA4);
- }
-
- pascal void MyInitWindows(void)
- {
- InitWindPP iw;
- long oldA4;
-
- oldA4 = SetCurrentA4();
- iw = (InitWindPP) oldInitWindows;
-
- if (!bypass)
- Cleanup();
-
- SetA4(oldA4);
-
- (*iw)();
- }
-
- static void Cleanup(void)
- {
- StopDraw();
-
- isRunning = false;
-
- if ((0 < LMGetCurApName()[0]) && (LMGetCurApName()[0] < sizeof(Str31)))
- {
- bypass = true;
-
- DoneDraw();
-
- // Now, put the old addresses into the JMP addresses, so that we are fully bypassed
- *(long*) (sizeof(short) + (Ptr) myGetPictureJMPAddr) = (long) oldGetPicture;
- *(long*) (sizeof(short) + (Ptr) myDrawPictureJMPAddr) = (long) oldDrawPicture;
- *(long*) (sizeof(short) + (Ptr) myInitWindowsJMPAddr) = (long) oldInitWindows;
- }
- }
-